package com.dingqb.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.dingqb.mycs.ui.entity.Dimension;

/**
 * 高度包括状态栏高度
 * 
 * http://blog.csdn.net/xinhai657/article/details/16863679
 * @author DingQingbo
 * 
 *
 */
public class ScreenWH {

	// 通过WindowManager获取屏幕高度 
	public static int getScreenHeight(Activity context) {
		DisplayMetrics dm = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.heightPixels;
	}

	public static int getScreenWidth(Activity context) {
		DisplayMetrics dm = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	// 通过Resources获取屏幕高度

	public static int getScreenHeight2(Activity context) {
		DisplayMetrics dm2 = context.getResources().getDisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm2);
		return dm2.heightPixels;
	}

	public static int getScreenWidth2(Activity context) {
		DisplayMetrics dm2 = context.getResources().getDisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm2);
		return dm2.widthPixels;
	}

	// 获取屏幕高度

	@SuppressWarnings("deprecation")
	public static int getScreenHeight3(Activity context) {
		Display display = context.getWindowManager().getDefaultDisplay();
		return display.getHeight();
	}

	@SuppressWarnings("deprecation")
	public static int getScreenWidth3(Activity context) {
		Display display = context.getWindowManager().getDefaultDisplay();
		return display.getWidth();
	}

	// px 与dp关系（0.75 / 1.0 / 1.5/2.0/3.0）
	// * 120-160 mdpi 1x 160
	// * 160-240 hdpi 1.5x 240
	// * 240-320 xhdpi 2x 320 720x1280
	// * 320-480 xxhdpi 3x 480 1080x1920
	// * 480-640 xxxdpi 4x 640
	public static float getScreenDensity(Activity context) {
		DisplayMetrics metric = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(metric);
		float density = metric.density; // 屏幕密度（0.75 / 1.0 / 1.5）
		return density;
	}

	// 获取屏幕密度 : 屏幕密度DPI（120 / 160 / 240）
	// * 120-160 mdpi 1x 160
	// * 160-240 hdpi 1.5x 240
	// * 240-320 xhdpi 2x 320 720x1280
	// * 320-480 xxhdpi 3x 480 1080x1920
	// * 480-640 xxxdpi 4x 640
	public static int getScreenDPI(Activity context) {
		DisplayMetrics metric = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(metric);
		int densityDpi = metric.densityDpi; // 屏幕密度DPI（120 / 160 / 240）
		return densityDpi;
	}

	/**
	 * 状态栏 全屏
	 * 
	 * @param context
	 * @param window
	 * @param view
	 */
	@SuppressLint("NewApi")
	private static void fullScreen(Activity context, Window window, View view) {
		// 透明状态栏
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
		// 透明状态栏
		context.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		// 透明导航栏
		context.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

		view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
	}

	/**
	 * 获得屏幕高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.widthPixels;
	}

	/**
	 * 获得屏幕宽度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.heightPixels;
	}

	/**
	 * 获得状态栏的高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getStatusHeight(Context context) {
		int statusHeight = -1;
		try {
			Class<?> clazz = Class.forName("com.android.internal.R$dimen");
			Object object = clazz.newInstance();
			int height = Integer.parseInt(clazz.getField("status_bar_height")
					.get(object).toString());
			statusHeight = context.getResources().getDimensionPixelSize(height);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return statusHeight;
	}

	/**
	 * 获取当前屏幕截图，包含状态栏
	 * 
	 * @param activity
	 * @return
	 */
	public static Bitmap snapShotWithStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		int width = getScreenWidth(activity);
		int height = getScreenHeight(activity);
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
		view.destroyDrawingCache();
		return bp;

	}

	/**
	 * 获取当前屏幕截图，不包含状态栏
	 * 
	 * @param activity
	 * @return
	 */
	public static Bitmap snapShotWithoutStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		int width = getScreenWidth(activity);
		int height = getScreenHeight(activity);
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
				- statusBarHeight);
		view.destroyDrawingCache();
		return bp;

	}

	/**
	 * 屏幕高度包括状态栏
	 * 
	 * @param activity
	 * @return
	 */
	@SuppressLint("NewApi")
	private Dimension getAreaOne(Activity activity) {
		Dimension dimen = new Dimension();
		Display disp = activity.getWindowManager().getDefaultDisplay();
		Point outP = new Point();
		disp.getSize(outP);
		dimen.mWidth = outP.x;
		dimen.mHeight = outP.y;
		return dimen;
	}

	/**
	 * 屏幕高度去除状态栏,
	 * 
	 * @param activity
	 * @return
	 */
	private Dimension getAreaTwo(Activity activity) {
		Dimension dimen = new Dimension();
		Rect outRect = new Rect();
		activity.getWindow().getDecorView()
				.getWindowVisibleDisplayFrame(outRect);
		System.out.println("top:" + outRect.top + " ; left: " + outRect.left);
		dimen.mWidth = outRect.width();
		dimen.mHeight = outRect.height();
		return dimen;
	}

	/**
	 * 屏幕高度去除状态栏，去除标题栏（NoActionBar）
	 * 
	 * @param activity
	 * @return
	 */
	private Dimension getAreaThree(Activity activity) {
		Dimension dimen = new Dimension();
		// 用户绘制区域
		Rect outRect = new Rect();
		activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT)
				.getDrawingRect(outRect);
		dimen.mWidth = outRect.width();
		dimen.mHeight = outRect.height();
		// end
		return dimen;
	}

	/**
	 * 获取分辨率
	 * 
	 * @param context
	 * @return
	 */

	@SuppressLint("NewApi")
	public static String getResolution(Activity context) {
		Display dis = context.getWindowManager().getDefaultDisplay();
		Point p = new Point();
		dis.getSize(p);
		return p.x + "*" + p.y;
	}

}
